﻿#include <QFile>
#include "CSensorSericesCfgOper.h"
#include "CGlobal.h"
#include "Public.h"
#include "CSqlOper.h"

CSensorSericesCfgOper::CSensorSericesCfgOper(CDevDataSensorSeries* pDevData, QWidget *parent)
	:m_pDevData(pDevData), QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("声振温设备参数配置"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	if (m_pDevData != nullptr)
	{
		m_sDevGuid = pDevData->m_sDevGuid;
		setData(m_pDevData->m_sDevName, m_pDevData->m_sIp, m_pDevData->m_sUserName, m_pDevData->m_sPassWd, m_pDevData->m_sBeltName);
	}
	setStyleSheetFile();
	initConnect();
	initUi();
	m_nType = 1;
}

CSensorSericesCfgOper::CSensorSericesCfgOper(QString sDevGuid, QWidget *parent /*= nullptr*/)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("声振温设备参数配置"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	ui.groupBox->hide();
	m_sDevGuid = sDevGuid;
	setStyleSheetFile();
	initConnect();
	initUi();
	m_nType = 0;
}

CSensorSericesCfgOper::~CSensorSericesCfgOper()
{}

void CSensorSericesCfgOper::setData(QString sName, QString sIP, QString sUser, QString sPassWd, QString sBeltName)
{
	ui.lineEditBeltName->setText(sBeltName);
	ui.lineEditIp->setText(sIP);
	ui.lineEditUser->setText(sUser);
	ui.lineEditPassWd->setText(sPassWd);
	ui.lineEditName->setText(sName);
}

void CSensorSericesCfgOper::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CSensorSericesCfgOper::initConnect()
{
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CSensorSericesCfgOper::slotClose);
	connect(ui.pushButtonOk, &QPushButton::clicked, this, &CSensorSericesCfgOper::slotSave);
}

void CSensorSericesCfgOper::initUi()
{
	QStringList headers;
	headers << A2T("通道号") << A2T("通道名") << A2T("是否启用");
	ui.tableWidget->setColumnCount(3);
	ui.tableWidget->setHorizontalHeaderLabels(headers);
	ui.tableWidget->setColumnWidth(0, 50);
	ui.tableWidget->setColumnWidth(1, 200);
	ui.tableWidget->setRowCount(8);
	ui.tableWidget->verticalHeader()->setVisible(false);
	ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	CSqlOper::instance().selectChannelByDevGuid(m_sDevGuid, vecRecord, sErrMsg);
	for (int i = 0; i < vecRecord.size(); i++)
	{
		int nChannel = vecRecord[i].value("CHANNEL_NO").toString().toInt();
		QString sChannelName = vecRecord[i].value("CHANNEL_NAME").toString();
		int nEnable = vecRecord[i].value("IS_ENABLE").toString().toInt();
		QString sEnable;
		if (nEnable == 1)
		{
			sEnable = A2T("是");
		}
		else
		{
			sEnable = A2T("否");
		}
		QTableWidgetItem *item = new QTableWidgetItem(QString::number(nChannel));
		item->setFlags(item->flags() & ~Qt::ItemIsEditable);
		item->setTextAlignment(Qt::AlignCenter);
		ui.tableWidget->setItem(i, 0, item);  // 设置通道号
		QTableWidgetItem *itemName = new QTableWidgetItem(sChannelName);
		itemName->setTextAlignment(Qt::AlignCenter);
		ui.tableWidget->setItem(i, 1, itemName);  // 设置通道名
		QTableWidgetItem *itemEnable = new QTableWidgetItem(sEnable);
		ui.tableWidget->setItem(i, 2, itemEnable);  // 设置是否启用
		itemEnable->setTextAlignment(Qt::AlignCenter);
	}
}

void CSensorSericesCfgOper::slotClose()
{
	this->close();
}

void CSensorSericesCfgOper::slotSave()
{
	if (m_nType == 1)
	{
		QString sName = ui.lineEditName->text();
		QString sIp = ui.lineEditIp->text();
		QString sUser = ui.lineEditUser->text();
		QString sPassWd = ui.lineEditPassWd->text();
		QString sBeltName = ui.lineEditBeltName->text();
		QString sErrMsg = "";
		bool bRet = CSqlOper::instance().updateSensorSeries(m_sDevGuid, sName, sIp, sUser, sPassWd, sBeltName, sErrMsg);
		QVector<SChannel> vecChannel;
		for (int i = 0; i < ui.tableWidget->rowCount(); i++)
		{
			SChannel stChannel;
			stChannel.nChannelNo = ui.tableWidget->item(i, 0)->text().toInt();
			stChannel.sChannelName = ui.tableWidget->item(i, 1)->text();
			if (ui.tableWidget->item(i, 2)->text() == A2T("是"))
			{
				stChannel.nIsEnable = 1;
			}
			else if (ui.tableWidget->item(i, 2)->text() == A2T("否"))
			{
				stChannel.nIsEnable = 0;
			}
			if (m_pDevData->m_mapChannel.contains(stChannel.nChannelNo))
			{
				m_pDevData->m_mapChannel[stChannel.nChannelNo] = stChannel;
			}
			vecChannel.push_back(stChannel);
		}
		bRet = CSqlOper::instance().updateChannelInfo(m_sDevGuid, vecChannel, sErrMsg);
		if (bRet == false)
		{
			messageBox(1, A2T("保存失败，原因:%1").arg(sErrMsg));
		}
		else
		{
			m_pDevData->m_sDevName = sName;
			m_pDevData->m_sIp = sIp;
			m_pDevData->m_sBeltName = sBeltName;
			m_pDevData->m_sPassWd = sPassWd;
			m_pDevData->m_sUserName = sUser;
			messageBox(0, A2T("保存成功"));
			this->accept();
		}
		emit(m_pDevData->sigCfgChanged());
	}
	else if (m_nType == 0)
	{
		QString sName = ui.lineEditName->text();
		QString sIp = ui.lineEditIp->text();
		QString sUser = ui.lineEditUser->text();
		QString sPassWd = ui.lineEditPassWd->text();
		QString sBeltName = ui.lineEditBeltName->text();
		QString sErrMsg = "";
		bool bRet = CSqlOper::instance().insertSensorSeries(m_sDevGuid, sName, sIp, sUser, sPassWd, sBeltName, sErrMsg);
		if (bRet == false)
		{
			messageBox(1, A2T("保存失败，原因:%1").arg(sErrMsg));
		}
		else
		{
			messageBox(0, A2T("保存成功"));
			this->accept();
		}
	}
}
